<?php

/**
 * Object/InstructionPaie.php
 */

namespace RHX\Model\Object;

use Zend\Db\Adapter\Adapter;

class InstructionPaie {

    /**
     * Clé primaire
     *
     * @var int
     */
    public $id;

    /**
     * Group d'appartenance de l'instruction de paie
     * 
     * @var int
     */
    public $group;

    /**
     * Matricule de l'employé (auto)
     *
     * @var int
     */
    public $matricule;

    /**
     * Anciennete durant la période
     * 
     * @var float
     */
    public $anciennete;

    /**
     * Mensualité (auto)
     * 
     * @var float
     */
    public $mensualite;

    /**
     * heures complémentaires (auto)
     *
     * @var float
     */
    public $heure_comptabilise;

    /**
     * Heures complémentaire et heure supplémentaire 25% (auto)
     *
     * @var float
     */
    public $heure_comptabilise_heure_supplementaire;

    /**
     * Heures supplémentaires 50 %
     *
     * @var float
     */
    public $heure_supplementaire;

    /**
     * (auto)
     *
     * @var float
     */
    public $ferie;

    /**
     *
     * @var string
     */
    public $heure_positive_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $dimanches;

    /**
     * (auto)
     *
     * @var float
     */
    public $heure_negative;

    /**
     *
     * @var string
     */
    public $heure_negative_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $am1_jour;

    /**
     * (auto)
     *
     * @var float
     */
    public $am1_heure;

    /**
     * (auto)
     *
     * @var float
     */
    public $heure_module_m1;

    /**
     * (auto)
     *
     * @var float
     */
    public $heure_module_css;

    /**
     *
     * @var string
     */
    public $css_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $laboratoire_heure;

    /**
     * (auto)
     *
     * @var float
     */
    public $entree_sortie_heure;

    /**
     * (auto)
     *
     * @var float
     */
    public $at_heure;

    /**
     * (auto)
     *
     * @var float
     */
    public $cm_heure;

    /**
     * (auto)
     *
     * @var float
     */
    public $am2_heure;

    /**
     *
     * @var string
     */
    public $maintient_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $cp_jour;

    /**
     * (auto)
     *
     * @var float
     */
    public $cp_heure;

    /**
     *
     * @var string
     */
    public $cp_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $prime_montant;

    /**
     *
     * @var string
     */
    public $prime_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $prime_montant_hors_cp;

    /**
     *
     * @var string
     */
    public $prime_libelle_hors_cp;

    /**
     * (auto)
     *
     * @var float
     */
    public $participation;

    /**
     * (auto)
     *
     * @var float
     */
    public $hors_temps_travail_montant;

    /**
     *
     * @var string
     */
    public $hors_temps_travail_libelle;

    /**
     * (auto)
     *
     * @var int
     */
    public $repas;

    /**
     * (auto)
     *
     * @var float
     */
    public $mutuelle;

    /**
     *
     * @var float
     */
    public $vehicule;

    /**
     *
     * @var float
     */
    public $logement;

    /**
     *
     * @var float
     */
    public $ijss_brute_montant;

    /**
     *
     * @var string
     */
    public $ijss_brute_libelle;

    /**
     * (auto)
     *
     * @var float
     */
    public $base_co;

    /**
     *
     * @var float
     */
    public $ijss_net;

    /**
     *
     * @var float
     */
    public $acompte;

    /**
     *
     * @var float
     */
    public $pret;

    /**
     *
     * @var float
     */
    public $atd;

    /**
     * (auto)
     *
     * @var float
     */
    public $changement_contrat;

    /**
     *
     * @var string
     */
    public $commentaire1;

    /**
     *
     * @var string
     */
    public $commentaire2;

    /**
     *
     * @var string
     */
    public $commentaire3;

    /**
     *
     * @var string
     */
    public $commentaire4;

    /**
     * (auto)
     * 
     * @var float
     */
    public $salaire_brute;

    /**
     * (auto)
     * 
     * @var float
     */
    public $total_heure;

    /**
     * Débogage
     * 
     * @var string
     */
    public $hc_debug;
    public $hs_debug;

    /**
     *
     * @var int
     */
    public $lock;
    
    /**
     *
     * @var int
     */
    public $mode_calcul;

    /**
     * Constante
     */
    const VIEW	 = 2;
    const CREATE	 = 1;

    /**
     *
     * @var Adapter $adapter
     */
    protected $adapter;

    /**
     * Remplissage
     *
     * @param array $data
     */
    public function exchangeArray($data) {
	$this->id					 = isset($data['id']) ? (int) $data['id'] : null;
	$this->group					 = isset($data['group']) ? (int) $data['group'] : 0;
	$this->matricule				 = isset($data['matricule']) ? (int) $data['matricule'] : 0;
	$this->anciennete				 = isset($data['anciennete']) ? $data['anciennete'] : 0.0;
	$this->mensualite				 = isset($data['mensualite']) ? $data['mensualite'] : 0.0;
	$this->heure_comptabilise			 = isset($data['heure_comptabilise']) ? $data['heure_comptabilise'] : 0.0;
	$this->heure_comptabilise_heure_supplementaire	 = isset($data['heure_comptabilise_heure_supplementaire']) ? $data['heure_comptabilise_heure_supplementaire'] : 0.0;
	$this->heure_supplementaire			 = isset($data['heure_supplementaire']) ? $data['heure_supplementaire'] : 0.0;
	$this->ferie					 = isset($data['ferie']) ? $data['ferie'] : 0.0;
	$this->heure_positive_libelle			 = isset($data['heure_positive_libelle']) ? $data['heure_positive_libelle'] : '';
	$this->dimanches				 = isset($data['dimanches']) ? $data['dimanches'] : 0.0;
	$this->heure_negative				 = isset($data['heure_negative']) ? $data['heure_negative'] : 0.0;
	$this->heure_negative_libelle			 = isset($data['heure_negative_libelle']) ? $data['heure_negative_libelle'] : '';
	$this->am1_jour					 = isset($data['am1_jour']) ? $data['am1_jour'] : 0;
	$this->am1_heure				 = isset($data['am1_heure']) ? $data['am1_heure'] : 0.0;
	$this->heure_module_m1				 = isset($data['heure_module_m1']) ? $data['heure_module_m1'] : 0.0;
	$this->heure_module_css				 = isset($data['heure_module_css']) ? $data['heure_module_css'] : 0.0;
	$this->css_jour					 = isset($data['css_jour']) ? $data['css_jour'] : 0;
	$this->css_libelle				 = isset($data['css_libelle']) ? $data['css_libelle'] : '';
	$this->laboratoire_heure			 = isset($data['laboratoire_heure']) ? $data['laboratoire_heure'] : 0.0;
	$this->entree_sortie_heure			 = isset($data['entree_sortie_heure']) ? $data['entree_sortie_heure'] : 0.0;
	$this->at_heure					 = isset($data['at_heure']) ? $data['at_heure'] : 0.0;
	$this->cm_heure					 = isset($data['cm_heure']) ? $data['cm_heure'] : 0.0;
	$this->am2_heure				 = isset($data['am2_heure']) ? $data['am2_heure'] : 0.0;
	$this->maintient_libelle			 = isset($data['maintient_libelle']) ? $data['maintient_libelle'] : '';
	$this->cp_jour					 = isset($data['cp_jour']) ? $data['cp_jour'] : 0.0;
	$this->cp_heure					 = isset($data['cp_heure']) ? $data['cp_heure'] : 0.0;
	$this->cp_libelle				 = isset($data['cp_libelle']) ? $data['cp_libelle'] : '';
	$this->prime_montant				 = isset($data['prime_montant']) ? $data['prime_montant'] : 0.0;
	$this->prime_libelle				 = isset($data['prime_libelle']) ? $data['prime_libelle'] : '';
	$this->prime_montant_hors_cp			 = isset($data['prime_montant_hors_cp']) ? $data['prime_montant_hors_cp'] : 0.0;
	$this->prime_libelle_hors_cp			 = isset($data['prime_libelle_hors_cp']) ? $data['prime_libelle_hors_cp'] : '';
	$this->participation				 = isset($data['participation']) ? $data['participation'] : 0.0;
	$this->hors_temps_travail_montant		 = isset($data['hors_temps_travail_montant']) ? $data['hors_temps_travail_montant'] : 0.0;
	$this->hors_temps_travail_libelle		 = isset($data['hors_temps_travail_libelle']) ? $data['hors_temps_travail_libelle'] : '';
	$this->repas					 = isset($data['repas']) ? (int) $data['repas'] : 0;
	$this->mutuelle					 = isset($data['mutuelle']) ? $data['mutuelle'] : 0.0;
	$this->vehicule					 = isset($data['vehicule']) ? $data['vehicule'] : 0.0;
	$this->logement					 = isset($data['logement']) ? $data['logement'] : 0.0;
	$this->ijss_brute_montant			 = isset($data['ijss_brute_montant']) ? $data['ijss_brute_montant'] : 0.0;
	$this->ijss_brute_libelle			 = isset($data['ijss_brute_libelle']) ? $data['ijss_brute_libelle'] : '';
	$this->base_co					 = isset($data['base_co']) ? $data['base_co'] : 0.0;
	$this->ijss_net					 = isset($data['ijss_net']) ? $data['ijss_net'] : 0.0;
	$this->acompte					 = isset($data['acompte']) ? $data['acompte'] : 0.0;
	$this->pret					 = isset($data['pret']) ? $data['pret'] : 0.0;
	$this->atd					 = isset($data['atd']) ? $data['atd'] : 0.0;
	$this->changement_contrat			 = isset($data['changement_contrat']) ? $data['changement_contrat'] : '';
	$this->commentaire1				 = isset($data['commentaire1']) ? $data['commentaire1'] : '';
	$this->commentaire2				 = isset($data['commentaire2']) ? $data['commentaire2'] : '';
	$this->commentaire3				 = isset($data['commentaire3']) ? $data['commentaire3'] : '';
	$this->commentaire4				 = isset($data['commentaire4']) ? $data['commentaire4'] : '';
	$this->salaire_brute				 = isset($data['salaire_brute']) ? $data['salaire_brute'] : 0.0;
	$this->total_heure				 = isset($data['total_heure']) ? $data['total_heure'] : 0.0;
	$this->lock					 = isset($data['lock']) ? (int) $data['lock'] : 0;
	$this->mode_calcul					 = isset($data['mode_calcul']) ? (int) $data['mode_calcul'] : 0;
    }

    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     * @param array $data
     */
    public function __construct(Adapter $adapter, $data = array()) {
	$this->adapter = $adapter;
	$this->exchangeArray($data);
    }

    /**
     * Récupérer l'employé en cours
     * 
     * @return Employe
     */
    public function getEmploye() {
	$tableEmploye = new \RHX\Model\Table\EmployeTable($this->adapter);
	try {
	    $employe = $tableEmploye->get($this->matricule);
	    return $employe;
	}
	catch (\Exception $ex) {
	    return null;
	}
    }

    /**
     * Récupérer la periode de l' Instruction
     * 
     * @return Periode
     */
    public function getPeriode() {
	//tables
	$tableInstGroup	 = new \RHX\Model\Table\InstructionPaieGroupTable($this->adapter);
	$tablePeriode	 = new \RHX\Model\Table\PeriodeTable($this->adapter);

	$oGroup = $this->getGroup();

	try {
	    $oPrd = $tablePeriode->get($oGroup->periode_id);
	    return $oPrd;
	}
	catch (\Exception $ex) {
	    return null;
	}
    }

    /**
     * Récupérer l'objet InstructionPayeGroup d'une Instruction
     * 
     * @return InstructionPayeGroup
     */
    public function getGroup() {
	$tableInstGroup = new \RHX\Model\Table\InstructionPaieGroupTable($this->adapter);

	try {
	    $grpInst = $tableInstGroup->get($this->group);
	    return $grpInst;
	}
	catch (\Exception $ex) {
	    return null;
	}
    }

    /**
     * Remplissage des données
     *
     * @param array $data
     */
    public function fill(Array $data = array()) {
	$this->matricule				 = isset($data['matricule']) ? $data['matricule'] : 0;
	$this->anciennete				 = isset($data['anciennete']) ? $data['anciennete'] : 0.0;
	$this->mensualite				 = isset($data['mensualite']) ? $data['mensualite'] : 0.0;
	$this->total_heure				 = isset($data['total_heure']) ? $data['total_heure'] : 0.0;
	$this->ferie					 = isset($data['ferie']) ? $data['ferie'] : 0;
	$this->dimanches				 = isset($data['dimanche']) ? $data['dimanche'] : 0;
	$this->heure_negative				 = isset($data['heure_negative']) ? $data['heure_negative'] : 0;
	$this->am1_jour					 = isset($data['am1_jour']) ? $data['am1_jour'] : 0;
	$this->am1_heure				 = isset($data['am1_heure']) ? $data['am1_heure'] : 0;
	$this->heure_module_css				 = isset($data['css']) ? $data['css'] : 0;
	$this->css_jour					 = isset($data['css_jour']) ? $data['css_jour'] : 0;
	$this->at_heure					 = isset($data['at']) ? $data['at'] : 0;
	$this->cm_heure					 = isset($data['cm']) ? $data['cm'] : 0;
	$this->am2_heure				 = isset($data['am2']) ? $data['am2'] : 0;
	$this->cp_heure					 = isset($data['cp_heure']) ? $data['cp_heure'] : 0;
	$this->cp_jour					 = isset($data['cp_jour']) ? $data['cp_jour'] : 0;
	$this->repas					 = isset($data['repas']) ? (int) $data['repas'] : 0;
	$this->heure_comptabilise			 = isset($data['heure_comptabilise']) ? $data['heure_comptabilise'] : 0.0;
	$this->heure_comptabilise_heure_supplementaire	 = isset($data['heure_comptabilise_heure_supplementaire']) ? $data['heure_comptabilise_heure_supplementaire'] : 0.0;
	$this->hc_debug					 = isset($data['hc_debug']) ? $data['hc_debug'] : null;
	$this->hs_debug					 = isset($data['hs_debug']) ? $data['hs_debug'] : null;
	$this->entree_sortie_heure			 = isset($data['entree_sortie_heure']) ? $data['entree_sortie_heure'] : $this->entree_sortie_heure;

	if (isset($data['regul'])) {
	    $this->heure_module_m1 = $data['regul'];
	}
	else { // ne pas ecraser            
	}
    }

    /**
     * 
     * @return type
     */
    public function getPrimeCP() {
	$tablePrime	 = new \RHX\Model\Table\PrimeTable($this->adapter);
	$cp		 = $this->prime_libelle;
	return $tablePrime->select(function(\Zend\Db\Sql\Select $select)use($cp) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->like('libelle', $cp);
		    $select->where($where);
		})->current();
    }

    /**
     * Copie tableau
     * 
     * @return array
     */
    public function getArrayCopy() {
	return get_object_vars($this);
    }

}
